//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.app.service.BusinessException;
import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.app.service.RaceService;
import com.ruoyi.live.base.domain.*;
import com.ruoyi.live.base.mapper.LiveExamMapper;
import com.ruoyi.live.base.mapper.TaskUserLearnLogMapper;
import com.ruoyi.live.base.mapper.UserMapper;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.register.utils.RegisterContant;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Controller
public class RaceController {
    @Autowired
    RaceService service;
    @Autowired
    UserMapper userMapper;
    @Autowired
    TaskUserLearnLogMapper taskUserLearnLogMapper;
    @Autowired
    LiveExamMapper examDao;
    @Autowired
    CacheService cacheService;

    public RaceController() {
    }

    @RequestMapping({"/race/live/index"})
    public ModelAndView raceLiveIndex() {
        String version = RegisterContant.versionID;
        List<LiveExam> exams = this.examDao.selectList((Wrapper)null);
        return (new ModelAndView("expert/index")).addObject("user", SessionInfo.getUser()).addObject("exam", exams.get(0)).addObject("version", version).addObject("useWorldRule", this.cacheService.getParam("USE_WORLD_RULE"));
    }

    @RequestMapping({"/open/race/g"})
    public String g() {
        return "/race/g";
    }

    @RequestMapping({"/race/index"})
    public String index() {
        return "/race/index";
    }

    @RequestMapping({"/race/detail"})
    public String detail() {
        return "/race/race_detail";
    }

    @RequestMapping({"/race/task/{id}"})
    public String task(@PathVariable Long id) {
        return "/race/race_task_" + id;
    }

    @ResponseBody
    @RequestMapping({"/race/loadLeftMenu"})
    public List<Task> loadLeftMenu() {
        return this.service.loadLeftMenu();
    }

    @ResponseBody
    @RequestMapping({"/race/getTaskUserId"})
    public Message getTaskUserId(Long taskId) {
        try {
            return new Message(true, this.service.getTaskUserId(taskId) + "");
        } catch (BusinessException var3) {
            return new Message(false, var3.getMessage());
        } catch (Exception var4) {
            var4.printStackTrace();
            return new Message(false, "操作失败");
        }
    }

    @ResponseBody
    @PostMapping({"/race/saveTaskUserData"})
    public Message saveTaskUserData(Long taskUserId, String data) {
        try {
            this.service.saveTaskUserData(taskUserId, data);
            return new Message(true, "保存成功");
        } catch (BusinessException var4) {
            var4.printStackTrace();
            return new Message(false, var4.getMessage());
        } catch (Exception var5) {
            var5.printStackTrace();
            return new Message(false, "保存失败");
        }
    }

    @ResponseBody
    @PostMapping({"/race/getTaskUserData"})
    public Message getTaskUserData(Long taskUserId) {
        return this.service.getTaskUserData(taskUserId);
    }

    @ResponseBody
    @PostMapping({"/race/uploadRaceVideo"})
    public Message uploadRaceVideo(Long taskUserId) {
        return this.service.getTaskUserData(taskUserId);
    }

    @ResponseBody
    @RequestMapping({"/race/uploadRaceFile"})
    public Message uploadRaceFile(Long taskUserId, MultipartFile file, HttpServletRequest request) {
        try {
            this.service.uploadRaceFile(taskUserId, file, request);
            return new Message(true, "上传成功");
        } catch (BusinessException var5) {
            var5.printStackTrace();
            return new Message(false, var5.getMessage());
        } catch (Exception var6) {
            var6.printStackTrace();
            return new Message(false, "上传失败");
        }
    }

    @ResponseBody
    @RequestMapping({"/race/finishExam"})
    public Message finishExam() {
        this.userMapper.updateById((new User()).setId(SessionInfo.getUserId()).setUserExamState(2));
        return new Message(true, "交卷成功");
    }

    @ResponseBody
    @RequestMapping({"/race/getFinishExam"})
    public Message getFinishExam() {
        User info = (User)this.userMapper.selectById(SessionInfo.getUserId());
        return Objects.equals(info.getUserExamState(), 2) ? new Message(true, "") : new Message(false, "");
    }

    @RequestMapping({"/race/expert/index"})
    public String raceExpertIndex() {
        return "/race/race_expert/expert_index";
    }

    @RequestMapping({"/race/expert/detail"})
    public String raceExpertDetail() {
        return "/race/race_expert/expert_detail";
    }

    @RequestMapping({"/race/expert/table"})
    public String raceExpertTable() {
        return "/race/race_expert/expert_table";
    }

    @ResponseBody
    @RequestMapping({"/race/expert/loadDatas"})
    public List<TaskExpert> loadDatas() {
        return this.service.doLoadDatas();
    }

    @ResponseBody
    @PostMapping({"/race/loadTaskData"})
    public Message loadTaskData(Long userId, Long taskId) {
        if (!ICommons.isNullOrEmpty(userId) && !ICommons.isNullOrEmpty(taskId)) {
            TaskUserLearnLog learnLog = (TaskUserLearnLog)this.taskUserLearnLogMapper.selectOne(new QueryWrapper((new TaskUserLearnLog()).setCreateUser(userId).setTaskId(taskId)));
            return learnLog == null ? new Message(false, "选手未提交答案") : new Message(true, learnLog.getTaskUserData());
        } else {
            return new Message(false, "参数缺失");
        }
    }

    @ResponseBody
    @PostMapping({"/race/setOneScore"})
    public Message setOneScore(TaskExpert expert) {
        return this.service.setOneScore(expert);
    }

    @ResponseBody
    @PostMapping({"/race/loadScores"})
    public TaskExpert loadScores(Long id) {
        return this.service.loadScores(id);
    }

    @ResponseBody
    @PostMapping({"/race/finishSubmitJudge"})
    public Message finishSubmitJudge(Long id) {
        try {
            this.service.finishSubmitJudge(id);
            return new Message(true, "评分成功");
        } catch (BusinessException var3) {
            var3.printStackTrace();
            return new Message(false, var3.getMessage());
        } catch (Exception var4) {
            var4.printStackTrace();
            return new Message(false, "评分失败");
        }
    }
}
